from django_filters import rest_framework as filters
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import status

from inventory.serializers import WarehouseSerializer
from inventory.models import Warehouse
from utils.custom_model_viewSet import CustomModelViewSet


class WarehouseFilter(filters.FilterSet):
    """
    仓库过滤类，用于仓库列表过滤
    """
    warehouse_code = filters.CharFilter(field_name='warehouse_code', lookup_expr='icontains')
    warehouse_name = filters.CharFilter(field_name='warehouse_name', lookup_expr='icontains')
    contact_person = filters.CharFilter(field_name='contact_person', lookup_expr='icontains')
    
    class Meta:
        model = Warehouse
        fields = [
            'warehouse_id', 'warehouse_code', 'warehouse_name', 'status', 
            'manager_id', 'is_primary', 'contact_person', 'contact_phone'
        ]


class WarehouseViewSet(CustomModelViewSet):
    """
    仓库视图集，提供仓库的增删改查操作
    """
    queryset = Warehouse.objects.filter(is_deleted=False).order_by('warehouse_code')
    serializer_class = WarehouseSerializer
    filterset_class = WarehouseFilter
    search_fields = ['warehouse_code', 'warehouse_name', 'contact_person']
    ordering_fields = ['warehouse_code', 'create_time', 'warehouse_id']
    ordering = ['warehouse_code']
    enable_soft_delete = True
    
    @action(detail=False, methods=['post'], url_path='import')
    def import_data(self, request):
        """
        导入仓库数据
        此方法需要处理Excel文件中的仓库数据导入
        """
        # 此处预留导入功能的实现
        # 当前端调用导入功能时，会触发此API
        # 具体实现将在后续开发中完成
        return Response({
            "code": 0,
            "message": "导入功能已预留",
            "data": None
        }, status=status.HTTP_200_OK)